package com.mybatis.mapper;

import com.mybatis.po.Order;
import com.mybatis.po.User;
import jdk.jfr.Enabled;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * ClassName: OrderMapper
 * Package: com.mybatis.mapper
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/11/11 19:42
 * @Version 1.0
 */
public interface OrderMapper {

    /**
     * 查询所有订单(按需加载订单关联的用户信息)
     * //@One相当于<association></association>
     * @return
     */
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "user_id",property = "userId"),
            @Result(property = "user",column = "user_id",javaType = User.class,
            one = @One(select = "com.mybatis.mapper.UserMapper.findById",fetchType = FetchType.LAZY))
    })
    @Select("SELECT * FROM mybatis03.`order`")
    List<Order> findOrderAndUserLazy();

    /**
     * 查询所有订单(延迟加载订单关联的用户信息和订单关联的订单明细)
     * @Many相当于collection标签
     * @return
     */
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "user_id",property = "userId"),
            @Result(property = "user",column = "user_id",javaType = User.class,
                    one = @One(select = "com.mybatis.mapper.UserMapper.findById",fetchType = FetchType.LAZY)),
            @Result(property = "orderDetails",column = "id",javaType = List.class,
                    many = @Many(select = "com.mybatis.mapper.OrderDetailMapper.findByOrderId",fetchType = FetchType.LAZY))
    })
    @Select("SELECT * FROM mybatis03.`order`")
    List<Order> findOrdersAndOrderDetailLazy();

}
